﻿package mortal.game.view.copy.ZZXYBattle
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.*;
    import mortal.mvc.core.*;

    public class ZZXYBattleController extends CopyControllerBase
    {
        private var _module:ZZXYBattleMoudle;
        private var _infoPanel:ZZXYBattleInfoPanel;
        private var _rewardModule:ZZXYBattleRewardModule;
        private var _activeOpening:Boolean = false;
        private var _itemsNum:Dictionary;
        private var _closeTime:int = -111111;
        private var _sceneUpdateTime:int = -111111;
        private var roundShowed:int = -1;
        public static var openData:SPublicCopyOpen;
        public static var myRoomId:int = -1;

        public function ZZXYBattleController()
        {
            this._itemsNum = new Dictionary();
            return;
        }// end function

        public function get rewardModule() : ZZXYBattleRewardModule
        {
            if (this._rewardModule == null)
            {
                this._rewardModule = new ZZXYBattleRewardModule();
            }
            return this._rewardModule;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            this.roundShowed = -1;
            super.enterCopy(param1);
            this._itemsNum = new Dictionary();
            if (this._infoPanel == null)
            {
                this._infoPanel = new ZZXYBattleInfoPanel();
            }
            this._infoPanel.show();
            if (this._module != null && !this._module.isHide)
            {
                this._module.hide();
            }
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            if (this._infoPanel != null)
            {
                this._infoPanel.hide();
                this._infoPanel.dispose(false);
                this._infoPanel = null;
            }
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.ZZXYBattleShowModule, this.showHideModuleHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicOpenPeaceBoss, this.activeOpenHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicClosePeaceBoss, this.activeCloseHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPeaceBossSelfScore, this.selfScoreHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPeaceBossScore, this.rankUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPeaceBossBattleBegin, this.canEnterBattleHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicRoulette, this.cardRewardHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPeaceBossRefreshEnd, this.bossRefreshEndHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.checkPlayerLevelupHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.packItemAddHandler);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            return;
        }// end function

        private function sceneUpdateHandler(param1) : void
        {
            this._sceneUpdateTime = getTimer();
            this.checkAndShowEndAlert();
            return;
        }// end function

        private function checkAndShowEndAlert() : void
        {
            var _loc_1:* = getTimer();
            if (_loc_1 - this._sceneUpdateTime > 4000 || _loc_1 - this._closeTime > 4000)
            {
                return;
            }
            Alert.show(Language.getString(20976));
            return;
        }// end function

        private function packItemAddHandler(param1:Array) : void
        {
            var _loc_2:* = int(param1[1]);
            if (_loc_2 != UpdateCode.EUpdateCodePeaceBossDrop)
            {
                return;
            }
            var _loc_3:* = param1[0] as ItemData;
            var _loc_4:* = int(param1[2]);
            if (this._itemsNum[_loc_3.itemCode] == null)
            {
                this._itemsNum[_loc_3.itemCode] = _loc_4;
            }
            else
            {
                this._itemsNum[_loc_3.itemCode] = this._itemsNum[_loc_3.itemCode] + _loc_4;
            }
            if (this._rewardModule != null && !this._rewardModule.isHide)
            {
                this._rewardModule.updateNum();
            }
            return;
        }// end function

        public function getNum() : int
        {
            var _loc_1:* = undefined;
            var _loc_2:ItemData = null;
            for (_loc_1 in this._itemsNum)
            {
                
                _loc_2 = new ItemData(int(_loc_1));
                if (_loc_2.itemInfo != null && ItemsUtil.isStrengthTone(_loc_2))
                {
                    return this._itemsNum[_loc_1];
                }
            }
            return 0;
        }// end function

        private function checkPlayerLevelupHandler(param1 = null) : void
        {
            if (!this._activeOpening || openData == null)
            {
                return;
            }
            if (cache.role.roleInfo.level < GameDefConfig.instance.getSystemOpenLevel(79))
            {
                return;
            }
            this.showActive();
            return;
        }// end function

        private function bossRefreshEndHandler(param1:MessageBlock) : void
        {
            var mb:* = param1;
            if (cache.role.roleInfo.level < GameDefConfig.instance.getSystemOpenLevel(79))
            {
                return;
            }
            with ({})
            {
                {}.close = function (param1:int) : void
            {
                if (param1 != Alert.OK)
                {
                    return;
                }
                if (CopyUtil.isInZZXYBattleCopy)
                {
                    GameProxy.copy.leaveCopy();
                }
                return;
            }// end function
            ;
            }
            Alert.show(Language.getString(20977), null, Alert.OK, null, function (param1:int) : void
            {
                if (param1 != Alert.OK)
                {
                    return;
                }
                if (CopyUtil.isInZZXYBattleCopy)
                {
                    GameProxy.copy.leaveCopy();
                }
                return;
            }// end function
            );
            return;
        }// end function

        private function activeOpenHandler(param1:MessageBlock) : void
        {
            this._activeOpening = true;
            myRoomId = -1;
            openData = param1.messageBase as SPublicCopyOpen;
            if (cache.role.roleInfo.level < GameDefConfig.instance.getSystemOpenLevel(79))
            {
                return;
            }
            GameProxy.copy.zzxyBattleGetRoomList();
            this.showActive();
            return;
        }// end function

        private function showActive() : void
        {
            var _loc_1:int = 0;
            if (openData == null)
            {
                return;
            }
            if (openData.endDt.time < ClockManager.instance.nowDate.time)
            {
                return;
            }
            ZZXYBattleIcon.instance.show();
            if (openData.openDt != null)
            {
                _loc_1 = Math.ceil((openData.openDt.time - ClockManager.instance.nowDate.time) / 1000);
                if (_loc_1 > 0)
                {
                    ZZXYBattleIcon.instance.setLeftTimeAndState(_loc_1, 1);
                }
                else if (openData.endDt != null)
                {
                    _loc_1 = Math.ceil((openData.endDt.time - ClockManager.instance.nowDate.time) / 1000);
                    ZZXYBattleIcon.instance.setLeftTimeAndState(_loc_1, 2);
                }
            }
            if (this._infoPanel != null && !this._infoPanel.isHide)
            {
                this._infoPanel.updateLeftTime();
            }
            return;
        }// end function

        private function activeCloseHandler(param1:MessageBlock) : void
        {
            this._activeOpening = false;
            myRoomId = -1;
            if (ZZXYBattleIcon.hasInstance())
            {
                ZZXYBattleIcon.instance.hide();
            }
            if (CopyUtil.isInZZXYBattleCopy)
            {
                this._closeTime = getTimer();
                if (this._module != null && !this._module.isHide)
                {
                    this._module.hide();
                }
                this.checkAndShowEndAlert();
            }
            return;
        }// end function

        private function selfScoreHandler(param1:MessageBlock) : void
        {
            var _loc_2:int = 0;
            if (this._infoPanel != null && !this._infoPanel.isHide)
            {
                _loc_2 = (param1.messageBase as SPublicInt).publicInt;
                this._infoPanel.updateSelfScore(_loc_2);
            }
            return;
        }// end function

        private function rankUpdateHandler(param1:MessageBlock) : void
        {
            var _loc_3:Array = null;
            var _loc_2:* = param1.messageBase as SPeaceBossScore;
            if (this._infoPanel != null && !this._infoPanel.isHide)
            {
                _loc_3 = _loc_2.playerScore;
                this._infoPanel.updateRankList(_loc_3);
            }
            if (_loc_2.round != this.roundShowed)
            {
                this.roundShowed = _loc_2.round;
                MsgManager.showTaskTarget(Language.getStringByParam(20978, this.roundShowed));
            }
            return;
        }// end function

        private function cardRewardHandler(param1:MessageBlock) : void
        {
            var _loc_5:Array = null;
            var _loc_6:ItemData = null;
            var _loc_9:int = 0;
            var _loc_10:ZZXYBattleCardData = null;
            var _loc_2:* = param1.messageBase as SDropRoulette;
            var _loc_3:* = ItemConfig.instance.getConfig(_loc_2.giftBagCode) as ItemGiftBagInfo;
            var _loc_4:* = _loc_3.itemList1.split("&");
            var _loc_7:* = _loc_4.length;
            var _loc_8:* = new Array();
            while (_loc_9 < _loc_7)
            {
                
                if (_loc_4[_loc_9])
                {
                    _loc_5 = _loc_4[_loc_9].split("#");
                    _loc_10 = new ZZXYBattleCardData();
                    _loc_10.itemCode = _loc_5[1];
                    _loc_10.num = _loc_5[2];
                    _loc_10.belongsToMe = _loc_10.itemCode == _loc_2.rewardCode && _loc_10.num == _loc_2.amount;
                    _loc_8.push(_loc_10);
                }
                _loc_9++;
            }
            this.rewardModule.show();
            this.rewardModule.updateRewardDatas(_loc_8, _loc_2.giftBagUid, _loc_2.wave);
            return;
        }// end function

        private function canEnterBattleHandler(param1:MessageBlock) : void
        {
            var _loc_5:int = 0;
            this._activeOpening = true;
            var _loc_2:* = param1.messageBase as SPeaceBossRoomList;
            var _loc_3:* = _loc_2.roomList;
            myRoomId = _loc_2.roomId;
            if (this._module != null && !this._module.isHide)
            {
                this._module.updateRoomInfos(_loc_3);
            }
            if (cache.role.roleInfo.level < GameDefConfig.instance.getSystemOpenLevel(79))
            {
                return;
            }
            var _loc_4:* = ClockManager.instance.nowDate.time;
            if (this._activeOpening && openData != null && openData.endDt != null && openData.openDt.time <= _loc_4 && openData.endDt.time - 500 >= _loc_4)
            {
                _loc_5 = Math.ceil((openData.endDt.time - ClockManager.instance.nowDate.time) / 1000);
                ZZXYBattleIcon.instance.show();
                ZZXYBattleIcon.instance.setLeftTimeAndState(_loc_5, 2);
            }
            return;
        }// end function

        private function showHideModuleHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as Boolean;
            if (event.data is String)
            {
                _loc_2 = event.data == "false" ? (false) : (true);
            }
            this.showModule(_loc_2);
            return;
        }// end function

        private function showModule(param1:Boolean) : void
        {
            if (param1)
            {
                if (this._module == null)
                {
                    this._module = new ZZXYBattleMoudle();
                }
                this._module.show();
            }
            else if (this._module != null && !this._module.isHide)
            {
                this._module.hide();
            }
            return;
        }// end function

        override protected function addListeners() : void
        {
            return;
        }// end function

        override protected function removeListeners() : void
        {
            return;
        }// end function

    }
}
